package gopensocial.test.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import gopensocial.client.GOpenSocial;
import gopensocial.client.Person;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Main implements EntryPoint {
	private Person me;
	private Map friends = new HashMap();

	public void onModuleLoad() {
		RootPanel rootPanel = RootPanel.get();
		
		final GOpenSocial opensocial = new GOpenSocial(){
			public void onGetViewer(Person person) {
				me = person;
				Window.alert(person.getName() + " " +  person.getId());
			}
			public void onFail(){
				Window.alert("Fail2");
			}
			
			public void onGetFriends(List listPerson){
				for (Iterator iterator = listPerson.iterator(); iterator
						.hasNext();) {
					Person person = (Person) iterator.next();
					friends.put(person.getId(), person);
					Window.alert(person.getName());
				}
			}
		    public void onGetViewerAppData(String id, String field, String value){
		    	Person person = (Person)friends.get(id);
		    	if(person !=null){
		    		Window.alert(person.getName()+ " " +field + " " + value);
		    	}
		    	else{
		    		Window.alert(id + " " +field + " " + value);
		    	}
		    	
		    }
			
		};
		opensocial.setInstance(opensocial);


		final Button getViewerButton = new Button();
		rootPanel.add(getViewerButton, 5, 56);
		getViewerButton.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				opensocial.getViewer();
			}
		});
		getViewerButton.setText("Get Viewer");

		final Button getFriendsButton = new Button();
		rootPanel.add(getFriendsButton, 5, 102);
		getFriendsButton.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				opensocial.getFriends();
			}
		});
		getFriendsButton.setText("Get Viewer Friends");

		final Button setScoreButton = new Button();
		rootPanel.add(setScoreButton, 5, 153);
		setScoreButton.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				long score = System.currentTimeMillis();
				Window.alert("score = " + score);
				opensocial.updateViewerAppData("score", ""+ score);
			}
		});
		setScoreButton.setText("Set Data");

		final Button getScoreViewerButton = new Button();
		rootPanel.add(getScoreViewerButton, 2, 197);
		getScoreViewerButton.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				opensocial.getViewerAppData("score");
			}
		});
		getScoreViewerButton.setText("Get Data Viewer");

		final Button getOwnerFriendsButton = new Button();
		rootPanel.add(getOwnerFriendsButton, 5, 243);
		getOwnerFriendsButton.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				opensocial.getPersonAppData("score");
			}
		});
		getOwnerFriendsButton.setText("Get Owner Friends Data");

	}
}
